package cn.ulc.socket.admin.service.coupon.impl;

import cn.hutool.core.convert.Convert;
import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.CalendarUtil;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.coupon.CouponDTO;
import cn.ulc.socket.admin.model.dto.coupon.CouponListDTO;
import cn.ulc.socket.admin.model.param.coupon.CouponSaveParam;
import cn.ulc.socket.admin.service.coupon.CouponServ;
import cn.ulc.store.dao.CouponDao;
import cn.ulc.store.entity.Coupon;
import lombok.AllArgsConstructor;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@AllArgsConstructor
public class CouponServImpl extends BaseService implements CouponServ {

    CouponDao couponDao;

    @Override
    public CouponListDTO findListPage(String title, String progressStatus, PaginationParam param) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",title);
        condition.put("progressStatus",progressStatus);
        PageQuery pageQuery = initPageQuery(param);
        pageQuery.setParas(condition);
        couponDao.findListPage(pageQuery);
        return new CouponListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public Coupon save(CouponSaveParam param) {
        Coupon entity = new Coupon();
        setBaseInfo(entity);
        entity.setStatus(AvailableStatus.AVAILABLE);
        return saveInfo(entity,param);
    }

    @Override
    public Coupon update(Long id, CouponSaveParam param) {
        long cid = Convert.toLong(id,0l);
        Coupon entity = couponDao.findOne(cid);
        if(entity == null) throw new PlatformException("未获取到优惠券信息!");
        setBaseInfo(entity);
        return saveInfo(entity,param);
    }

    public Coupon saveInfo(Coupon entity,CouponSaveParam param){
        entity.setTitle(param.getTitle());
        entity.setType(param.getType());
        entity.setIsLimit(param.getIsLimit());
        entity.setNum(param.getNum());
        entity.setIsLimitReceive(param.getIsLimitReceive());
        entity.setReceiveNum(param.getReceiveNum());
        entity.setIsForever(param.getIsForever());
        if(param.getIsForever() == false){
            Date startDate = CalendarUtil.stringToDate(param.getStartDate(),CalendarUtil.SDF_DEFAULT_FULL);
            Date endDate = CalendarUtil.stringToDate(param.getEndDate(),CalendarUtil.SDF_DEFAULT_FULL);
            Date date = new Date();
            entity.setStartDate(startDate);
            entity.setEndDate(endDate);
            //未开始
            if(date.compareTo(startDate) < 0) entity.setProgressStatus(PubDataConstant.COUPON_STATUS_NOT_START);
            //已结束
            if(date.compareTo(endDate) > 0) entity.setProgressStatus(PubDataConstant.COUPON_STATUS_ENDED);
        }else{
            entity.setProgressStatus(PubDataConstant.COUPON_STATUS_USING);
        }
        entity.setIsFull(param.getIsFull());
        entity.setFullAmount(param.getFullAmount());
        entity.setUserScope(param.getUserScope());
        entity.setAmount(param.getAmount());
        if(PubDataConstant.COUPON_SCOPE_GOODS.equals(param.getUserScope())){
            entity.setGoodsId(param.getGoodsId());
        }
        if(PubDataConstant.COUPON_SCOPE_TYPE.equals(param.getUserScope())){
            entity.setGoodsTypeId(param.getGoodsTypeId());
        }
        entity.setRemark(param.getRemark());
        couponDao.save(entity);
        return entity;
    }

    @Override
    public CouponDTO findDetail(Long id) {
        long cid = Convert.toLong(id,0l);
        Coupon entity = couponDao.findDetail(cid);
        return new CouponDTO(entity);
    }

    @Override
    public Boolean changeStatus(Long id, AvailableStatus status) {
        long cid = Convert.toLong(id,0l);
        Coupon entity = couponDao.findOne(cid);
        if(entity == null) throw new PlatformException("获取优惠券信息失败!");
        entity.setStatus(status);
        setBaseInfo(entity);
        couponDao.save(entity);
        return true;
    }

    @Override
    public Boolean deleted(Long id) {
        long cid = Convert.toLong(id,0l);
        Coupon entity = couponDao.findOne(cid);
        if(entity != null) {
            setBaseInfo(entity);
            entity.setDeleted(DeletedStatus.DELETED);
            couponDao.save(entity);
        }
        return true;
    }
}
